/*
 * Copyright 2013 Sigurd Randoll.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package de.digiway.rapidbreeze.server.migrate;

import de.digiway.rapidbreeze.server.config.ServerConfiguration;
import de.digiway.rapidbreeze.server.infrastructure.objectstorage.ObjectStorage;
import de.digiway.rapidbreeze.server.infrastructure.objectstorage.migration.MigrateScript;
import de.digiway.rapidbreeze.server.migrate.util.StorageProviderDefault;
import de.digiway.rapidbreeze.server.model.storage.StorageProvider;
import de.digiway.rapidbreeze.server.model.storage.StorageProviderRepository;
import de.digiway.rapidbreeze.server.model.storage.StorageProviderRepositoryFactory;
import de.digiway.rapidbreeze.server.model.storage.provider.ShareOnlineBiz;
import de.digiway.rapidbreeze.shared.rest.config.DownloadSpeed;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;

/**
 *
 * @author Sigurd
 */
public class V001_DataInit implements MigrateScript {

    @Override
    public void execute(ObjectStorage objectStorage) {
        try {
            setupConfiguration();
            setupShareOnlineBiz();
        } catch (IOException ex) {
            throw new IllegalStateException("Cannot create download folder.", ex);
        }
    }

    private void setupConfiguration() throws IOException {
        // Download folder:
        ServerConfiguration config = ServerConfiguration.getInstance();
        String home = System.getProperty("user.home");
        Path path = Paths.get(home, "downloads");
        Files.createDirectories(path);
        config.setDownloadTargetFolder(path);

        // Setup speed:
        config.setDownloadSpeed(DownloadSpeed.STOP);

        // Setup parallel downloads:
        config.setParallelDownloads(DownloadSpeed.STOP, 0);
        config.setParallelDownloads(DownloadSpeed.SLOW, 1);
        config.setParallelDownloads(DownloadSpeed.NORMAL, 2);
        config.setParallelDownloads(DownloadSpeed.FAST, 4);

        // Setup speed per state:
        config.setKbPerSecond(DownloadSpeed.STOP, 0);
        config.setKbPerSecond(DownloadSpeed.SLOW, 100);
        config.setKbPerSecond(DownloadSpeed.NORMAL, 500);
        config.setKbPerSecond(DownloadSpeed.FAST, 0);
    }

    private void setupShareOnlineBiz() {
        StorageProviderRepository providerRepository = StorageProviderRepositoryFactory.getInstance();
        StorageProvider provider = providerRepository.registerProvider("ShareOnlineBiz", ShareOnlineBiz.class);
        provider.setPremiumUsername(StorageProviderDefault.getUsername(ShareOnlineBiz.class));
        provider.setPremiumPassword(StorageProviderDefault.getPassword(ShareOnlineBiz.class));
        providerRepository.updateStorageProvider(provider);
    }
}
